package com.ljh.util;

import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileTraverse {
    List<String> imageList=new ArrayList<String>();
    public List<String> fileTraverse(){
        String path = ServletActionContext.getServletContext().getRealPath("upload")+"/";		//要遍历的路径
        File file = new File(path);		//获取其file对象
        func(file);
        return imageList;
    }

    public void func(File file){
        File[] fs = file.listFiles();
        for(File f:fs){
            if(f.isDirectory())	//若是目录，则递归打印该目录下的文件
                func(f);
            if(f.isFile()){
                imageList.add(ServletActionContext.getRequest().getScheme() + "://" + ServletActionContext.getRequest().getServerName() + ":" + ServletActionContext.getRequest().getServerPort() +ServletActionContext.getRequest().getContextPath()+"/"+f.getAbsolutePath().substring(f.getAbsolutePath().indexOf("upload")).replaceAll("\\\\","/"));
            }
        }
    }
}
